package com.fc.app.fcapp.pages.recycler

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.fc.app.fcapp.R

/**
 * Created by fc on 2017/10/30.
 */
class MyRecyclerViewAdapter(datas: List<Int>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val mDatas: List<Int>? = datas


    //     enum class ITEM_TYPE {
    private val TYPE_TEXT = 1
    private val TYPE_PNG = 2
//    }

    override fun getItemViewType(position: Int): Int {
//        return super.getItemViewType(position)
        if (position == 10)
            return TYPE_PNG
        else
            return TYPE_TEXT
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder? {
        if (viewType == TYPE_TEXT) {
            val view = LayoutInflater.from(parent?.context).inflate(R.layout.recycle_item_text, parent, false)
            return TextHolder(view)
        } else if (viewType == TYPE_PNG) {
            val view = LayoutInflater.from(parent?.context).inflate(R.layout.recycle_item_png, parent, false)
            return PngHolder(view)
        }

        return null
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        if (holder is TextHolder && position != 10)
            mDatas?.get(position)?.let { holder.mText.text = it.toString() }
//            textView?.setText(mDatas?.get(position))
        else if (holder is PngHolder)
            mDatas?.get(10).let {
                holder.mImage.setBackgroundResource(it!!)
            }
    }

    override fun getItemCount(): Int {
        return mDatas?.size!!
    }


    class TextHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var mText: TextView

        init {
            mText = itemView?.findViewById<TextView>(R.id.recycle_textView)!!
        }
    }

    class PngHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var mImage: ImageView

        init {
            mImage = itemView?.findViewById<ImageView>(R.id.recycle_imageView)!!
        }
    }

    internal inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//        var mText: TextView

        init {
//            mText = itemView.findViewById(R.id.item_tx)
        }
    }


}